const Comment = require('../models/comments')

class CommentCtl {
    async find(ctx){
        const { per_page = 10 } = ctx.query
        const page = Math.max(ctx.query.page * 1 -1,1)  
        const perPage = Math.max(per_page * 1,1)
        const q = new RegExp(ctx.query.q)
        const {questionId,answerId} = ctx.params
        const {rootCommentId} = ctx.query
        ctx.body = await Comment
          .find({content:q,questionId,answerId,rootCommentId}) 
          .limit(perPage).skip(page*perPage)
          .populate('commentator replyTo');  //头像URL
    }
    async checkCommentExist(ctx,next){
        const comment = await Comment.findById(ctx.params.id).select('+commentator')  //select 
        if(!comment){ ctx.throw(404,'评论不存在') }
        //答案的问题id与问题id   &&表示只有删改查时候才检查此逻辑，赞、踩答案不会
        if(ctx.params.questionId && ctx.params.questionId !== comment.questionId){ctx.throw(404,'该问题下没有此评论')}
        if(ctx.params.answersId && ctx.params.answersId !== comment.answersId){ctx.throw(404,'该答案下没有此评论')}
        ctx.state.comment = comment;   
        await next();
    }
    async findById(ctx){
        const {fields=''} = ctx.query;   
        const selectFields = fields.split(';').filter(f=>f).map(f => '+' + f).join('');
        const comment = await Comment.findById(ctx.params.id).select(selectFields).populate('commentator');   //populate 
        ctx.body = comment;
    }
    async create(ctx){
        ctx.verifyParams({
            content: {type:'string',required:true},
            rootCommentId: {type:'string',required:false},
            replyTo: {type:'string',required:false},
        })
        const commentator = ctx.state.user._id
        const {answerId,questionId}= ctx.params
        const comment = await new Comment({...ctx.request.body, commentator, questionId,answerId }).save();
        ctx.body = comment;
    }
    //确定提问者  和  修改的用户 是否一致 
    async checkCommentator(ctx,next){
        const {comment} = ctx.state
        if(comment.commentator.toString() !== ctx.state.user._id){ctx.throw(403,'没有权限')}
        await next();
    }
    async update(ctx){
        ctx.verifyParams({
            content: {type:'string',required:true},
        })
        const {content} = ctx.request.body
        await ctx.state.comment.update({content})
        ctx.body = ctx.state.comment;
    }
    async delete(ctx){
        await Comment.findByIdAndRemove( ctx.params.id );
        ctx.status = 204;
    }
}

module.exports = new CommentCtl();